home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / RISCOS2 / TCP_131S.ARC / c / ttydriv < prev    next >
Text File  |  1993-12-22  |  6KB  |  175 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "global.h"
  4. #include "misc.h"
  5.  
  6. /* TTY input driver */
  7.  
  8. int ttymode;
  9. #define TTY_COOKED      0
  10. #define TTY_RAW         1
  11.  
  12. int ttyecho = 1;
  13. #define TTY_NOECHO      0
  14. #define TTY_ECHO        1
  15.  
  16. #define LINESIZE        256
  17.  
  18. #define ESC     29
  19. #define CTLR    18
  20. #define CTLW    23
  21. #define F3      0x183
  22.  
  23. void raw(void)
  24. {
  25.         ttymode = TTY_RAW;
  26. }
  27.  
  28. void cooked(void)
  29. {
  30.         ttymode = TTY_COOKED;
  31. }
  32.  
  33. void echo(void)
  34. {
  35.         ttyecho = TTY_ECHO;
  36. }
  37.  
  38. void noecho(void)
  39. {
  40.         ttyecho = TTY_NOECHO;
  41. }
  42.  
  43. /* Accept characters from the incoming tty buffer and process them
  44.  * (if in cooked mode) or just pass them directly (if in raw mode).
  45.  * Returns the number of characters available for use; if non-zero,
  46.  * also stashes a pointer to the character(s) in the "buf" argument.
  47.  */
  48.  /*Control-R added by df for retype of lines - useful in Telnet */
  49.  /*Then df got impatient and added Control-W for erasing words  */
  50.  /*Then g1emm got even more impatient and added F-3 line recall.*/
  51.  
  52. int ttydriv(int c, char **buf)
  53. {
  54.         static char linebuf[LINESIZE];
  55.         static char *cp = linebuf;
  56.         char *rp ;
  57.         static int tty_done_init = 0;
  58.         int cnt;
  59.         int seenprint ;
  60.  
  61.         if(buf == (char **)NULL)
  62.                 return 0;       /* paranoia check */
  63.  
  64.         if(!tty_done_init)
  65.         {
  66.                 for(cp = linebuf; cp < &linebuf[LINESIZE]; *cp++ = '\r');
  67.                 cp = linebuf;
  68.                 tty_done_init = 1;
  69.         }
  70.  
  71.         cnt = 0;
  72.         switch(ttymode)
  73.         {
  74.         case TTY_RAW:
  75.                 if (c < 128)
  76.                 {
  77.                         *cp++ = c;
  78.                         cnt = cp - linebuf;
  79.                         cp = linebuf;
  80.                 }
  81.                 break;
  82.         case TTY_COOKED:
  83.                 /* Perform cooked-mode line editing */
  84.                 switch(c){
  85.                 case '\r':      /* CR and LF are equivalent */
  86.                 case '\n':
  87.                         *cp++ = '\r';
  88.                         *cp++ = '\n';
  89.                         cwprintf(NULL, "\r\n");
  90.                         cnt = cp - linebuf;
  91.                         cp = linebuf;
  92.                         break;
  93.                 case '\b':              /* Backspace */
  94.                 case 0x7F:              /* Delete */
  95.                         if(cp != linebuf)
  96.                         {
  97.                                 cp--;
  98.                                 if (ttyecho)
  99.                                         cwputs(NULL, "\b \b");
  100.                         }
  101.                         break;
  102.                 case CTLR:      /* print line buffer */
  103.                         if (ttyecho)
  104.                                 cwprintf(NULL, "^R");
  105.                         cwputs(NULL, "\r\n");
  106.                         if (ttyecho) {
  107.                                 rp = linebuf ;
  108.                                 while (rp < cp)
  109.                                         cwputchar(NULL, *rp++);
  110.                         }
  111.                         break ;
  112.                 case F3:      /* Repeat last line */
  113.                         while (*cp != '\n' && *cp != '\r' && cp < &linebuf[LINESIZE])
  114.                         {
  115.                                 if (*cp == 0)
  116.                                         *cp = ' ' ;
  117.                                 if (ttyecho)
  118.                                         cwputchar(NULL, *cp++) ;
  119.                         }
  120.                         if(cp != linebuf && *cp == '\n')
  121.                         {
  122.                                 cp--;
  123.                                 *cp = '\r';
  124.                                 if (ttyecho)
  125.                                         cwputchar(NULL, '\b'); /* to allow for one trailing space */
  126.                         }
  127.                         break ;
  128.                 case ESC:      /* Line kill */
  129.                         if (ttyecho) {
  130.                                 while(cp != linebuf)
  131.                                 {
  132.                                         cp--;
  133.                                         cwputchar(NULL, '\b');
  134.                                 }
  135.                         } else
  136.                                 cp = linebuf;
  137.                         break;
  138.                 case CTLW:      /* erase word */
  139.                         seenprint = 0 ; /* we haven't seen a printable char yet */
  140.                         while (cp != linebuf)
  141.                         {
  142.                                 cp--;
  143.                                 cwputchar(NULL, '\b') ;
  144.                                 if (isspace(*cp))
  145.                                 {
  146.                                         if (seenprint)
  147.                                                 break ;
  148.                                 }
  149.                                 else
  150.                                         seenprint = 1 ;
  151.                         }
  152.                         break ;
  153.                 default:        /* Ordinary character */
  154.                         *cp++ = c;
  155.                         if (ttyecho)
  156.                                 cwputchar(NULL, c);
  157.                         if(cp >= &linebuf[LINESIZE]){
  158.                                 cnt = cp - linebuf;
  159.                                 cp = linebuf;
  160.                         }
  161.                         break;
  162.                 }
  163.         }
  164.         if(cnt != 0)
  165.         {
  166.                 *buf = linebuf;
  167.                 for(rp = &linebuf[cnt]; rp < &linebuf[LINESIZE]; *rp++ = '\r');
  168.         }
  169.         else
  170.                 *buf = NULLCHAR;
  171.  
  172.         return cnt;
  173. }
  174.  
  175.